package com.qf.ability.message.utils;

import com.qf.ability.message.entity.Room;
import com.qf.ability.message.entity.UserSession;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 房间管理
 * author Ken
 * create_time 2022/10/17
 */
public class RoomUtils {

    private static Map<Integer, Room> rooms = new ConcurrentHashMap<>();

    public static void putRoom(Integer cid, Room room){
        rooms.put(cid, room);
    }

    public static Room get(Integer cid){
        Room room = rooms.get(cid);
        if (room == null) {
            room = new Room();
            putRoom(cid, room);
        }
        return room;
    }

    public static Room remove(Integer cid){
        Room remove = rooms.remove(cid);
        remove.getMaster().release();
        for (UserSession slave : remove.getSlaves().values()) {
            slave.release();
        }
        return remove;
    }
}
